﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using PMSA.Framework.Log;

public partial class Admin_Uc_ProductManager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            
        }
        else
        {
            CreateDDLGroupProduct();
            CreateDDLBrand();
        }
    }
    private void CreateDDLGroupProduct()
    {
        try
        {
            GroupProduct obj = new GroupProduct();
            IList<GroupProduct> list = obj.List(1, 1000, 0, 0, -1);
            chooseGroupProduct.DataValueField = "GroupProductId";
            chooseGroupProduct.DataTextField = "GroupProductName";
            chooseGroupProduct.DataSource = list;
            chooseGroupProduct.DataBind();
        }
        catch (Exception ex)
        {
            CLogManager.Write("ProductManager.CreateDDLGroupProduct", ex.ToString(), "ADMIN");
        }
    }
    private void CreateDDLBrand()
    {
        try
        {
            Brand obj = new Brand();
            IList<Brand> list = obj.List(1, 1000, 0);
            chooseBrand.DataValueField = "BrandId";
            chooseBrand.DataTextField = "BrandName";
            chooseBrand.DataSource = list;
            chooseBrand.DataBind();
        }
        catch (Exception ex)
        {
            CLogManager.Write("ProductManager.CreateDDLBrand", ex.ToString(), "ADMIN");
        }   
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Product obj = new Product();
            string name = proName.Text;
            string unit = proUnit.Text;
            string price = proPrice.Text;
            string des = proDes.Text;
            int active = proActive.Checked ? 1 : 0;
            int groupid = int.Parse(chooseGroupProduct.SelectedValue);
            int brandid = int.Parse(chooseBrand.SelectedValue);
            string _photoNull = "../img/layout/blank.gif";
            string _photoLink = "../Admin/Upload/Image/Product/";
            #region photos
            string _photos1 = _photoNull;
            if (!String.IsNullOrEmpty(photos1.Value))
            {
                _photos1 = _photoLink + photos1.Value;
            }
            string _photos2 = _photoNull;
            if (!String.IsNullOrEmpty(photos2.Value))
            {
                _photos2 = _photoLink + photos2.Value;
            }
            string _photos3 = _photoNull;
            if (!String.IsNullOrEmpty(photos3.Value))
            {
                _photos3 = _photoLink + photos3.Value;
            }
            string _photos4 = _photoNull;
            if (!String.IsNullOrEmpty(photos4.Value))
            {
                _photos4 = _photoLink + photos4.Value;
            }
            string _photos5 = _photoNull;
            if (!String.IsNullOrEmpty(photos5.Value))
            {
                _photos5 = _photoLink + photos5.Value;
            }
            #endregion
            obj.Insert(name, unit, int.Parse(price), des, active, groupid, brandid, _photos1, _photos2, _photos3, _photos4, _photos5);
        }
        catch (Exception ex)
        {
            CLogManager.Write("ProductManager.btnAdd_Click", ex.ToString(), "ADMIN");
        }
    }
}